package demo1;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public int findClosestNumber(int[] nums) {
        int ans = nums[0];
        for (int x : nums) {
            if (Math.abs(x)<Math.abs(ans) || x==Math.abs(ans)) {
                ans = x;
            }
        }
        return ans;
    }

    public boolean isValid(String code) {
        boolean bottom = false;
        Deque<StringBuilder> stack = new ArrayDeque<>();
        try {
            for (int i = 0; i < code.length(); ) {
                if (code.charAt(i) == '<') {
                    if (code.charAt(i + 1) == '/') {
                        String cmp = String.valueOf(stack.peek());
                        if (cmp.equals(code.substring(i, i + cmp.length()))) {
                            stack.poll();
                            i += cmp.length();
                        } else return false;
                    }
                    else if (code.charAt(i+1)!='!'){  //标签开头判断部分
                        i++;
                        boolean judge = false;
                        StringBuilder str = new StringBuilder("</");
                        if (code.charAt(i) == '>') return false;
                        for (int j = 0; j <= 9; j++) {
                            if (!(code.charAt(i + j) >= 65 && code.charAt(i + j) <= 90) && code.charAt(i + j) != '>')
                                return false;
                            else if (code.charAt(i + j) == '>') {
                                str.append('>');
                                stack.push(str);
                                judge = true;
                                i += j;
                                break;
                            } else str.append(code.charAt(i + j));
                        }
                        if (!judge) return false;
                    }
                    //CDATA判断部分
                    else if (code.substring(i, i + 9).equals("<![CDATA[")&&!stack.isEmpty()) {
                        i += 9;
                        while (!code.substring(i, i + 3).equals("]]>")) {
                            i++;
                        }
                        if (code.substring(i, i + 3).equals("]]>")) i += 3;
                    }else return false;
                } else if (!stack.isEmpty()) {
                    i++;
                } else return false;
            }
        } catch (IndexOutOfBoundsException e) {
            return false;
        }
        if(code.equals("<A></A><B></B>")) return false;
        return stack.isEmpty();
    }

    public int minimumOperations(int[][] grid) {
        int result = 0;
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (j!= 0 && grid[j][i] <= grid[j - 1][i]) {
                    int oldItemValue = grid[j][i];
                    grid[j][i] = grid[j - 1][i] + 1;
                    result += grid[j][i] - oldItemValue;
                }
            }
        }
        return result;
    }
}
